package com.wifi.user.servlet;

import com.alibaba.fastjson.JSON;
import com.wifi.unti.LoginUtil;
import com.wifi.user.dao.UserDao;
import com.wifi.user.entity.User;
import com.wifi.user.service.UserService;
import com.wifi.user.service.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/changePwd")
public class ChangePwdServlet extends HttpServlet {
    UserService userService=new UserServiceImpl();
    LoginUtil loginUtil=new LoginUtil();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//request.getRequestDispatcher("/WEB-INF/user/mana.jsp").forward(request,response);
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String id=request.getParameter("uid");
    String pwdOld=request.getParameter("pwdOld");
    String newPwd=request.getParameter("pwdNew");
    String   pwdNewCof=request.getParameter("pwdNewCof");

 if (pwdNewCof.isEmpty()||pwdOld.isEmpty()||newPwd.isEmpty())
 { response.getWriter().write("1"); }
 else if (!pwdNewCof.equals(newPwd))
 {response.getWriter().write("2");}

 else { Object o=userService.getUserByID(id);
     if (o!=null){ String pwd=loginUtil.encroMD5(pwdOld);if (((User)o).getPassword().equals(pwd))
     { if (pwdOld .equals(newPwd)) { response.getWriter().write("6");
         System.out.println(66);}
     else {Integer i = userService.updatePwd(loginUtil.encroMD5(newPwd), Integer.valueOf(id));
         if (i > 0) {response.getWriter().write("3"); }
         else { response.getWriter().write("4"); }}}
      else{
          response.getWriter().write("5");
      }
   }else {response.getWriter().write("4");}

 }
    }
}
